﻿
using System.Linq;
using FacebookXmpp.Parse;

namespace FacebookXmpp.Stanzas
{
	internal class StreamFeatures : Stanza
	{
		internal override StanzaListenCriteria ListenFor
		{
			get { return new StanzaListenCriteria( "stream:features" ); }
		}

        public override void ProcessResponse(MessageData recieved)
		{
            Stream.RemoveHandler(this);
            
			foreach (MessageData child in recieved.GetChildren())
			{
				switch (child.Name)
				{
					// StartTls MUST take priority over mechanisms\x-facebook-platform
                    case "starttls":
						Stream.AddHandler( new StartTls( ) );
                        return;

					case "mechanisms":
                        if (child.GetChildren().Any(md => md.Value.ToString() == "X-FACEBOOK-PLATFORM"))
							Stream.AddHandler( new AuthXFacebookPlaform( ) );
                        break;

                    case "bind":
						Stream.AddHandler( new Bind( ) );
                        break;

                    case "session":
						Stream.AddHandler( new Session( ) );
                        break;

				}
			}
		}
	}
}
